<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MySQL配置</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container {
            max-width: 800px;
            margin-top: 50px;
        }
        .card {
            margin-bottom: 20px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .btn-primary {
            margin-top: 20px;
        }
        .alert {
            margin-top: 20px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center mb-4">MySQL配置</h1>
        
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0">数据库连接参数</h5>
            </div>
            <div class="card-body">
                <form id="mysqlConfigForm">
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="host" class="form-label">主机地址</label>
                            <input type="text" class="form-control" id="host" name="host" value="localhost">
                        </div>
                        <div class="col-md-6">
                            <label for="port" class="form-label">端口号</label>
                            <input type="number" class="form-control" id="port" name="port" value="3306">
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="user" class="form-label">用户名</label>
                            <input type="text" class="form-control" id="user" name="user" value="root">
                        </div>
                        <div class="col-md-6">
                            <label for="password" class="form-label">密码</label>
                            <input type="password" class="form-control" id="password" name="password" value="root">
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="database" class="form-label">数据库名</label>
                            <input type="text" class="form-control" id="database" name="database" value="test1">
                        </div>
                        <div class="col-md-6">
                            <label for="charset" class="form-label">字符集</label>
                            <input type="text" class="form-control" id="charset" name="charset" value="utf8mb4">
                        </div>
                    </div>
                    <button type="button" class="btn btn-primary" id="saveConfig">保存配置</button>
                    <button type="button" class="btn btn-success ms-2" id="testConnection">测试连接</button>
                </form>
                <div class="alert alert-success" id="successAlert">操作成功！</div>
                <div class="alert alert-danger" id="errorAlert">操作失败！</div>
            </div>
        </div>
        
        <div class="d-flex justify-content-between mt-4">
            <a href="/static/index.html" class="btn btn-secondary">返回首页</a>
            <a href="/static/mysql_operate.html" class="btn btn-primary">数据库操作页面</a>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 页面加载时获取当前配置
            fetch('/api/module/mysql/config')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        const config = data.data;
                        document.getElementById('host').value = config.host;
                        document.getElementById('port').value = config.port;
                        document.getElementById('user').value = config.user;
                        document.getElementById('password').value = config.password;
                        document.getElementById('database').value = config.database;
                        document.getElementById('charset').value = config.charset;
                    }
                })
                .catch(error => {
                    console.error('获取配置失败:', error);
                });

            // 保存配置
            document.getElementById('saveConfig').addEventListener('click', function() {
                const formData = {
                    host: document.getElementById('host').value,
                    port: parseInt(document.getElementById('port').value),
                    user: document.getElementById('user').value,
                    password: document.getElementById('password').value,
                    database: document.getElementById('database').value,
                    charset: document.getElementById('charset').value
                };

                fetch('/api/module/mysql/config', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(formData)
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        showAlert('successAlert', '配置保存成功！');
                    } else {
                        showAlert('errorAlert', '配置保存失败：' + data.message);
                    }
                })
                .catch(error => {
                    showAlert('errorAlert', '保存配置请求失败');
                    console.error('保存配置失败:', error);
                });
            });

            // 测试连接
            document.getElementById('testConnection').addEventListener('click', function() {
                fetch('/api/module/mysql/test-connection', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        host: document.getElementById('host').value,
                        port: parseInt(document.getElementById('port').value),
                        user: document.getElementById('user').value,
                        password: document.getElementById('password').value,
                        database: document.getElementById('database').value,
                        charset: document.getElementById('charset').value
                    })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        showAlert('successAlert', '连接测试成功！');
                    } else {
                        showAlert('errorAlert', '连接测试失败：' + data.message);
                    }
                })
                .catch(error => {
                    showAlert('errorAlert', '连接测试请求失败');
                    console.error('测试连接失败:', error);
                });
            });

            // 显示提示信息
            function showAlert(elementId, message) {
                const alertElement = document.getElementById(elementId);
                alertElement.textContent = message;
                alertElement.style.display = 'block';
                
                // 3秒后自动隐藏
                setTimeout(() => {
                    alertElement.style.display = 'none';
                }, 3000);
            }
        });
    </script>
</body>
</html> 